package org.hot100_0.leetcode139;

import java.util.HashSet;
import java.util.List;

public class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {

        int n = s.length();

        boolean [] dp = new boolean[n+1];
        dp[0]=true;
        HashSet<String> hashSet = new HashSet<>();

        for (String str :wordDict) {
            hashSet.add(str);
        }

        for (int i = 1; i <=n; i++) {
            for (String str:wordDict) {
                if (i>=str.length() && dp[i-str.length()] && hashSet.contains(s.substring(i-str.length(),i))){
                    dp[i]=true;
                    break;
                }
            }
        }

        return dp[n];
    }
}
